<?php

defined('BY_JYA') or exit('Access Denied');
global $_RQ,$_AR;
$_RQ['op']=$_RQ['op']?$_RQ['op']:'detail';
if($_RQ['op']=='detail'){
	/*******main*******/
	$return['ischecklogin']=$this->model('member')->checklogin();
	if(intval($_RQ['fromuser'])){
		$this->model('member')->setfromuser();
	}
	$id = intval($_RQ['id']);
	if(!$id){
		exi('该庄园不存在!','error');
	}
	$this->session('cangid',$id);
	$team=pdo_get('jy_nongfee_cang',array('uniacid'=>$_SESSION['uniacid'],'id'=>$id));
	if($team && $team['status']==0){
		$this->model('model')->add_yao($team);
	}
	$team['isnum']=pdo_count('jy_nongfee_yao',array('cangid'=>$team['id'],'uniacid'=>$_SESSION['uniacid']),'id');
	$team['yao']=pdo_get('jy_nongfee_yao',array('cangid'=>$team['id'],'status'=>1,'uniacid'=>$_SESSION['uniacid']));
	if($team['yao']['id']){
		$team['isfee']=pdo_getsum('jy_nongfee_canlog',array('yaoid'=>$team['yao']['id'],'cangid'=>$team['id'],'uniacid'=>$_SESSION['uniacid']),'jifen');
		if($team['isfee']>0 && $team['yao']['alljifee']>0){
			$team['israte']=$team['isfee']/$team['yao']['alljifee'];
		}else{
			$team['israte']=0;
		}
		$team['yao']['statustitle']='种植中';
	}else{
		$team['yao']=pdo_get('jy_nongfee_yao',array('cangid'=>$team['id'],'status'=>0,'uniacid'=>$_SESSION['uniacid']),'','id asc');
		if($team['yao']['id']){
			$team['israte']=0;
			$team['yao']['statustitle']='预约中';
		}else{
			$team['yao']=pdo_get('jy_nongfee_yao',array('cangid'=>$team['id'],'uniacid'=>$_SESSION['uniacid']),'','id desc');
			$team['yao']['statustitle']='已结束';
		}
	}
	$team['nowyaonum']=pdo_count('jy_nongfee_yao',array('id <='=>$team['yao']['id'],'cangid'=>$team['id'],'uniacid'=>$_SESSION['uniacid']));
	$return['members']=$members;
	$team['statustitle']='已结束';
	$day=date('Y-m-d');
	if(!$team['status']){
				$start_time=$item['yao']['startime'];
				$end_time=strtotime($day.' 24:00:00');
				if(TIMESTAMP>$start_time){
					$team['isstart']=1;
					$rest_time=$team['yao']['endtime'];
				}else{
					$team['isstart']=0;
					$rest_time=$start_time;
					$dayhour=date('H:i',$start_time);
					$team['statustitle']=$dayhour.'开始';
				}
				$team['rest_time']=model('jy_weishop/model')->date($rest_time-TIMESTAMP);
	}
	$return['team']=$team;
	$return['list']=array();
	$return['post']=array('type'=>0,'num'=>0,'bei'=>1);
	$return['params']=array('status'=>0,'cangid'=>$team['id']);
	$return['pluginset']['jian_feetype']=$return['pluginset']['jian_feetype']?:3;
	$return['pluginset']['jian_feetypetitle']=$this->credit_type[$return['pluginset']['jian_feetype']];
	$return['sharedata']=array(
			'title'=>$nickname.'-发起种植收益啦！参与就可获利了！',
			'desc'=>'参与种植收益啦，满'.$team['mannum'].'人就可获利了！',
			'link'=>custom_url('jy_nongfee/detail//id='.$team['id'].'&fromuser='.$_SESSION['uid'],true),
			'imgUrl'=>tomedia($team['image'])
	);
	if($this->setting['tpl_type']){
		//$return['htmls']=$this->template_url('detail1');
	}
	set_html('title','种植详情');
	exi($return);	
}
if($_RQ['op']=='getlist'){
	$team=pdo_get('jy_nongfee_cang',array('uniacid'=>$_SESSION['uniacid'],'id'=>$this->session('cangid')));
	$pindex = max(1, intval($_RQ['page']));
	$psize = 15;
	$params=array('uniacid'=>$_SESSION['uniacid'],'cangid'=>$this->session('cangid'));
	if($_RQ['status']==3){
		$params['status']=array(3,4,6,7);
	}elseif($_RQ['status']==1){
		$params['status']=array(2,5);
	}else{
		$params['status']=array(0,1);
	}
    $orderby='paixu asc';
	$total='';
	$return['list']=pdo_getslice('jy_nongfee_yao',$params,array($pindex,$psize),$total,$fields,'',$orderby);	
	$jian_days=$this->setting['jian_days']?:3;
	$this->setting['jian_feerate']=$this->setting['jian_feerate']?:30;
	foreach($return['list'] as &$v){
			$this->model('model')->up_yao($v);
			if($v['status']==1 && $v['endtime']<=TIMESTAMP){
				$v['status']=3;
			}
			if($v['status'] <=1 && $team['status']==2){
				pdo_update('jy_nongfee_yao',array('status'=>7),array('id'=>$v['id'],'uniacid'=>$_SESSION['uniacid']));
				$v['status']=7;
			}
			if(!$v['status'] && $v['startime']<=TIMESTAMP){
				$v['status']=$this->model('model')->start_yao($v);
			}
			$v['title']=$team['title'].'第'.$v['paixu'].''.$this->y_title;
			$v['endtime']=date('Y-m-d',$v['endtime']);
			$v['startime']=date('Y-m-d',$v['startime']);
			$v['statustitle']=$this->yao_status[$v['status']];
			$v['isfee']=pdo_getsum('jy_nongfee_canlog',array('yaoid'=>$v['id'],'cangid'=>$this->session('cangid'),'uniacid'=>$_SESSION['uniacid']),'jifen');
			$v['israte']=$v['isfee']?price($v['isfee']*100/$v['alljifee']):0;
			$v['jian_getrate']=$team['jian_getrate'];
			$v['jian_jinmin']=$team['jian_jinmin'];
			$v['jian_jinmax']=$v['jian_jinmax']?:$team['jian_jinmax'];
			$v['jian_feetype']=$v['jian_jinmax']*$this->setting['jian_feerate']/100;
			$v['isyuenum']=pdo_count('jy_nongfee_yuelog',array('mid'=>$_SESSION['uid'],'yaoid'=>$v['id'],'cangid'=>$this->session('cangid'),'uniacid'=>$_SESSION['uniacid']),'id');
	}
	exi($return);
}
// 完成下单
if($_RQ['op']=='post'){
	$post=get_postdata();
	$yaoid=intval($post['yaoid']);
	$type=intval($post['type']);
	$cangid=$this->session('cangid');
	$post['jifen']=intval($post['jifen']);
	if(!$post['jifen']){
		exi('请输入要投入'.$this->cd_title.'额度,'.$this->cd_title.'必须大于等于1！','error');
	}
	$team=pdo_get('jy_nongfee_cang',array('uniacid'=>$_SESSION['uniacid'],'id'=>$cangid));
	$yao=pdo_get('jy_nongfee_yao',array('uniacid'=>$_SESSION['uniacid'],'id'=>$yaoid));
	$jian_jinmax=$yao['jian_jinmax'];
	$this->model('model')->check_buy($yao,$post['jifen'],$_RQ['isyue'],$type);
	if($post['jifen']>$jian_jinmax && $this->setting['jian_mid']!=$_SESSION['uid']){
		exi('投入额度不能大于'.$jian_jinmax.'！','error');
	}
	
	if($post['jifen']<$team['jian_jinmin'] && $this->setting['jian_mid']!=$_SESSION['uid']){
		exi('投入'.$this->cd_title.'额度不能小于'.$team['jian_jinmin'].'！','error');
	}
	$member=model('core/member')->detail($_SESSION['uid'],array('credit1','credit2','credit3','credit4','credit6','mobile','avatar','nickname'));
	$isfee=pdo_getsum('jy_nongfee_canlog',array('yaoid'=>$yao['id'],'uniacid'=>$_SESSION['uniacid']),'jifen');
	$lastfee=$yao['alljifee']-$isfee;
	if($post['jifen']>$lastfee || $lastfee<=0){
		exi('当前剩余'.$lastfee.'可投入'.$this->cd_title.'！','error');
	}
	if($post['bei']>1){
		if($post['credit1num']>$member['credit1']){
			exi('本次购买加倍包消耗'.$post['credit1num'].'积分，您的积分不足！','error');
		}
	}
	if($member['credit4']<$post['jifen']){
		exi('您的'.$this->cd_title.'不足，不能参与！','error');
	}
	$tag=$_RQ['isyue']?'预约种植':'参与种植';
	//参与种植'.$this->cd_title.'数量需消耗积分
	if($team['jian_xiaojinum']){
		$jian_xiaojinum=$post['jifen']*$team['jian_xiaojinum']/100;
		if($jian_xiaojinum>$member['credit1']){
			exi('本次'.$tag.'需消耗'.$jian_xiaojinum.'积分，您的积分不足，不能参与！','error');
		}
	}
	$alljifen=$jian_xiaojinum+$post['credit1num'];
	if($alljifen>$member['credit1']){
		exi('本次'.$tag.'需消耗'.$alljifen.'积分(包含购买加倍包)，您的积分不足，不能参与！','error');
	}
	$log=array(
			'cangid'=>$cangid,
			'yaoid'=>$yaoid,
			'type'=>$type,
			'jifen'=>$post['jifen'],
	);
	if($_RQ['isyue']){
		if($type){
			$nmember=pdo_get('jy_nongfee_member',array('mid'=>$_SESSION['uid'],'uniacid'=>$_SESSION['uniacid']));
			$quan_num=$this->setting['quan_num'];
			if($quan_num>$nmember['zquan']){
				exi('本次'.$tag.'需消耗'.$quan_num.'张种植券，您的种植券不足，不能预约！','error');
			}
			$quan_jifen=$this->setting['quan_jifen'];
			$allyueifen=$alljifen+$quan_jifen;
			if($allyueifen>$member['credit1']){
				exi('本次'.$tag.'需消耗'.$allyueifen.'积分(包含购买加倍包+消耗种植积分+预约消耗积分)，您的积分不足，不能预约！','error');
			}
		}else{
			$quan_yuejifen=$this->setting['quan_yuejifen'];
			$allyueifen=$alljifen+$quan_yuejifen;
			if($allyueifen>$member['credit1']){
				exi('本次'.$tag.'需消耗'.$allyueifen.'积分(包含购买加倍包+消耗种植积分+预约消耗积分)，您的积分不足，不能预约！','error');
			}
		}
		$this->model('model')->up_yuelog($log,$allyueifen,$post['credit1num'],$post['bei']);	
	}else{
		$this->model('model')->up_canlog($log,$jian_xiaojinum,$post['credit1num'],$post['bei']);
	}
	if($_RQ['isyue']){
		exi($tag.'成功！','','jy_nongfee/yuelog/');
	}else{
		exi($tag.'成功！','','jy_nongfee/canlog/');
	}
}
?>